package com.jingyanzi.privilege.service.impl;

import java.util.Calendar;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jingyanzi.privilege.dao.SecurityFieldMapper;
import com.jingyanzi.privilege.dao.SecurityRolefieldMapper;

import com.jingyanzi.privilege.domain.FieldDO;
import com.jingyanzi.privilege.domain.result.ResultDO;
import com.jingyanzi.privilege.domain.result.ResultSupport;

import com.jingyanzi.privilege.entity.SecurityField;
import com.jingyanzi.privilege.entity.SecurityFieldExample;
import com.jingyanzi.privilege.entity.SecurityFieldExample.Criteria;
import com.jingyanzi.privilege.entity.SecurityRolefieldExample;

import com.jingyanzi.privilege.query.FieldQueryDO;
import com.jingyanzi.privilege.service.IFieldService;
import com.jingyanzi.privilege.util.BeanUtilsExtends;

@Service("fieldService")
public class FieldServiceImpl extends BaseServiceImpl implements IFieldService {

	@Autowired
	private SecurityFieldMapper fieldMapper;
	@Autowired
	private SecurityRolefieldMapper rolefieldMapper;

	public ResultDO add(FieldDO filedDO) {

		SecurityField field = new SecurityField();

		ResultSupport result = BeanUtilsExtends.copy(field, filedDO);
		if (!result.isSuccess())
			return result;
		field.setCreatetime(Calendar.getInstance().getTime());

		int r = -1;
		try {
			r = fieldMapper.insertSelective(field);
		} catch (Exception e) {
			e.printStackTrace();
			//result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("添加字段信息失败");
			result.setSuccess(false);
			return result;
		}
		if (r < 1) {
			//result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("添加字段信息失败");
			result.setSuccess(false);
			return result;
		}
		result.setModel(ResultDO.FIRST_MODEL_KEY, field.getId());
		return result;
	}

	public ResultDO removeForTableId(FieldQueryDO query) {

		ResultSupport result = new ResultSupport();

		SecurityFieldExample example = new SecurityFieldExample();
		example.createCriteria().andTableidEqualTo(query.getTableId()).andIsdeleteEqualTo(1);

		int r = -1;
		List<SecurityField> list = null;
		try {
			list = fieldMapper.selectByExample(example);
		} catch (Exception e) {
			result.setErrorCode(ResultDO.FIELD_DELETE_ERROR);
			result.setErrorMsg("获取字段信息失败");
			result.setSuccess(false);
			return result;
		}

		if (list == null) {
			result.setErrorCode(ResultDO.FIELD_DELETE_ERROR);
			result.setErrorMsg("获取字段信息失败");
			result.setSuccess(false);
			return result;
		}

		for (SecurityField field : list) {
			try {
				r = removeMapping(field.getId());
				if (r < -1)
					new Exception();
				field.setTableid(0);
				field.setIsdelete(-1);
				r = fieldMapper.updateByPrimaryKeySelective(field);

			} catch (Exception e) {
				r = -1;
				//result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
				result.setErrorMsg("删除字段信息失败");
				result.setSuccess(false);
				return result;
			}
		}

		if (r < -1) {
			//result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("删除字段信息失败");
			result.setSuccess(false);
			return result;
		}

		return result;
	}

	public ResultDO remove(Integer id) {

		ResultSupport result = new ResultSupport();

		SecurityField field = new SecurityField();

		field.setId(id);
		field.setIsdelete(-1);

		int r = -1;

		r = removeMapping(id);

		if (r > -1) {
			r = -1;
			try {
				r = fieldMapper.updateByPrimaryKeySelective(field);
			} catch (Exception e) {
				//result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
				result.setErrorMsg("删除字段信息失败");
				result.setSuccess(false);
				return result;
			}
		}

		if (r < 1) {
			//result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("删除字段信息失败");
			result.setSuccess(false);
			return result;
		}

		return result;
	}

	public ResultDO modifi(FieldDO filedDO) {

		SecurityField field = new SecurityField();

		ResultSupport result = BeanUtilsExtends.copy(field, filedDO);
		if (!result.isSuccess())
			return result;

		int r = -1;
		try {
			r = fieldMapper.updateByPrimaryKeySelective(field);
		} catch (Exception e) {
			e.printStackTrace();
			//result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("修改字段信息失败");
			result.setSuccess(false);
			return result;
		}
		if (r < 1) {
			//result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("删除字段信息失败");
			result.setSuccess(false);
			return result;
		}

		return result;
	}

	public ResultDO get(Integer id) {

		ResultSupport result = new ResultSupport();

		if (id < 1) {
			result.setSuccess(false);
			return result;
		}

		SecurityField field = null;

		try {
			field = fieldMapper.selectByPrimaryKey(id);
		} catch (Exception e) {
			e.printStackTrace();
			//result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("删除字段信息失败");
			result.setSuccess(false);
			return result;
		}

		if (field == null) {
			result.setSuccess(false);
			//result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("删除字段信息失败");
			result.setSuccess(false);
			return result;
		}

		result.setModel(ResultSupport.FIRST_MODEL_KEY, getFieldDO(field));
		return result;
	}

	public ResultDO query(FieldQueryDO query) {

		ResultSupport result = new ResultSupport();

		List<SecurityField> list = null;

		SecurityFieldExample example = new SecurityFieldExample();
		Criteria createCriteria = example.createCriteria();
		if (query.getTableId() != 0) {
			createCriteria.andTableidEqualTo(query.getTableId());

		}
		if (StringUtils.isNotBlank(query.getNamecode())) {
			createCriteria.andNamecodeEqualTo(query.getNamecode());

		}

		createCriteria.andIsdeleteEqualTo(1);

		List<FieldDO> fieldDOList = null;

		if (query.isPage()) {
			example.setLimitByPage(query.getStart() + "," + query.getPageRows());
			long count = fieldMapper.countByExample(example);
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
		}

		try {
			list = fieldMapper.selectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
			//result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("获取字段信息失败");
			result.setSuccess(false);
			return result;
		}

		fieldDOList = getFieldDOList(list);
		result.setModel(ResultSupport.FIRST_MODEL_KEY, fieldDOList);

		return result;
	}

	/**
	 * 映射删除所有关系
	 * 
	 */
	private int removeMapping(int id) {

		int r = -1;

		SecurityRolefieldExample example2 = new SecurityRolefieldExample();

		example2.createCriteria().andFieldidEqualTo(id);

		try {
			r = rolefieldMapper.deleteByExample(example2);
		} catch (Exception e) {
			e.printStackTrace();
		}

		if (r < -1) {
			return r;
		}

		r = -1;

		return 1;
	}

}
